package b2.player
{
	import b2.player.dataModel.Mail;
	import b2.player.dataModel.Obj;
	import b2.player.dataModel.ObjWithNum;
	
	public class MailPackage
	{
		public var mailList:Object;
		
		public function MailPackage(packageXML:XML = null)
		{
			mailList = new Object();
			if (packageXML.mail != null && packageXML.mail.length() > 0)
			{
				for (var i:int = 0; i < packageXML.mail.length(); i++)
				{
					var fromId:String = packageXML.mail[i]["fromId"];
					var mailId:int = packageXML.mail[i]["mailId"];
					var message:String = packageXML.mail[i]["message"];
					var mail:Mail = new Mail(fromId, PlayerInfoManager.getInstance().myUserId.toString(), message, null);
					mail.fromName = packageXML.mail[i]["fromName"];
					if (packageXML.mail[i].gift == null)
						continue;
					for (var j:int = 0; j < packageXML.mail[i].gift.length(); j++)
					{
						var objectIndex:int = packageXML.mail[i].gift[j]["objectIndex"];
						var objectNum:int = packageXML.mail[i].gift[j]["objectNum"];
						var basicLevel:int = packageXML.mail[i].gift[j]["basicLevel"];
						var strengthenLevel:int = packageXML.mail[i].gift[j]["strengthenLevel"];
						var gift:ObjWithNum = new ObjWithNum(new Obj(objectIndex, basicLevel, strengthenLevel),objectNum);
						mail.giftArray.push(gift);
					}
					mailList[mailId] = mail;
				}
			}
		}
		
		public function getMailId(mail:Mail):int
		{
			var mailId:int = -1;
			for (var mailIdStr:String in mailList)
			{
				if (mail == mailList[mailIdStr])
					mailId = parseInt(mailIdStr);
			}
			return mailId;
		}
		
		public function deleteMail(mail:Mail):void
		{
			for (var mailIdStr:String in mailList)
			{
				if (mail == mailList[mailIdStr])
					delete mailList[mailIdStr];
			}
		}
	}
}